/*
 *
 * Created on 21.08.2013 10:09:47
 */
package org.opentcs.guing.components.properties.panel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JPanel;
import org.opentcs.guing.components.dialogs.DetailsDialogContent;
import org.opentcs.guing.components.properties.type.CoursePointProperty;
import org.opentcs.guing.components.properties.type.Property;
import org.opentcs.guing.model.SystemModel;
import org.opentcs.guing.model.elements.PointModel;
import org.opentcs.guing.util.ResourceBundleUtil;

/**
 * Panel that allows selection of a single point in the course model.
 *
 * @author Philipp Seifert (Fraunhofer IML)
 */
public class CoursePointPropertyEditorPanel
    extends JPanel
    implements DetailsDialogContent {

  private final List<String> pointNames = new ArrayList<>();
  private SystemModel fSystemModel;
  private CoursePointProperty fProperty;

  /**
   * Creates a new instance.
   */
  public CoursePointPropertyEditorPanel() {
    initComponents();
  }

  @Override
  public void updateValues() {
    fProperty.setPointName((String) pointComboBox.getSelectedItem());
  }

  @Override
  public String getTitle() {
    return ResourceBundleUtil.getBundle().getString("CoursePointPropertyEditorPanel.title");
  }

  @Override
  public void setProperty(Property property) {
    fProperty = (CoursePointProperty) property;

    // Update the list of point names from the system model.
    pointNames.clear();
    for (PointModel pointModel : fSystemModel.getPointModels()) {
      String pointName = pointModel.getName();
      pointNames.add(pointName);
    }

    Collections.sort(pointNames);
    updateView();
  }

  @Override
  public Property getProperty() {
    return fProperty;
  }

  @Override
  public void setSystemModel(SystemModel systemModel) {
    fSystemModel = systemModel;
  }

  private void updateView() {
    DefaultComboBoxModel<String> model
        = (DefaultComboBoxModel<String>) pointComboBox.getModel();
    model.removeAllElements();
    model.addElement(null);
  
    for (String pointName : pointNames) {
      model.addElement(pointName);
    }

    if (fProperty.getPointName() != null) {
      model.setSelectedItem(fProperty.getPointName());
    }
  }

  // CHECKSTYLE:OFF
  /**
   * This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

    pointComboBox = new javax.swing.JComboBox<String>();

        setMinimumSize(new java.awt.Dimension(150, 20));
        setPreferredSize(new java.awt.Dimension(150, 20));
        setLayout(new java.awt.GridBagLayout());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(5, 3, 5, 3);
        add(pointComboBox, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JComboBox<String> pointComboBox;
    // End of variables declaration//GEN-END:variables
  // CHECKSTYLE:ON
}
